<?php
/**
 * Created by PhpStorm.
 * User: luoqinghui
 * Date: 2020/7/24
 * Time: 上午17:40
 */

namespace App\Http\Controllers\Api\System;

use App\Http\Controllers\Controller;
use App\Modules\System\Services\NoticeService;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

/**
 * 系统通知
 */
class NoticeController extends Controller{
    /**
     * 系统通知列表
     */
    public function getList(Request $request){
        $size = $request->input('size',15);
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $result = NoticeService::getList(['user_id'=>$user_id,'size'=>$size]);
        DB::beginTransaction();
        try{
            //修改用户表系统通知最后查看时间
            $userSave = DB::table('users')->where('id',$user_id)->update(['system_notice_at'=>date('Y-m-d H:i:s')]);
            if ( empty($userSave) && $userSave !== 0 ) {
                throw new \Exception('SELECE_FAILURE');
            }
            //修改系统通知为已读
            $noticeSave = DB::table('notice')->where('recipient_id',$user_id)->update(['is_reading'=>1]);
            if ( empty($noticeSave) && $noticeSave !== 0 ) {
                throw new \Exception('SELECE_FAILURE');
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
        }
        return response()->pagination($result);
    }

    /**
     * 系统通知详情
     */
    public function getNotice(Request $request){
        $notice_id = $request->input('id',0);
        if ( empty($notice_id) ) return response()->error("PARAM_EMPTY");
        return response()->success(NoticeService::getNotice($notice_id));
    }

    /**
     * 添加系统通知
     */
    public function addNotice(Request $request){
        $recipient_id = $request->input('recipient_id',0);
        $title = $request->input('title','');
        $content = $request->input('content','');
        if ( empty($content) ) return response()->error("PARAM_EMPTY");
        //添加数据
        $noticeArr = [
            'recipient_id'  => $recipient_id,
            'title'         => $title,
            'content'       => $content,
        ];
        $result = NoticeService::addNotice($noticeArr);
        if ( empty($result) ) return response()->error("ADD_FAILURE");
        return response()->success();
    }

    /**
     * 修改系统通知
     */
    public function updateNotice(Request $request){
        $notice_id = $request->input('id',0);
        if ( empty($notice_id) ) return response()->error("PARAM_EMPTY");
        $dataUpdateArr = ['id'=>$notice_id];
        !isset($_POST['recipient_id']) ? : $dataUpdateArr['recipient_id'] = $request->input('recipient_id',0);
        !isset($_POST['title']) ? : $dataUpdateArr['title'] = $request->input('title','');
        !isset($_POST['content']) || empty($_POST['content']) ? : $dataUpdateArr['content'] = $request->input('content');
        $result = NoticeService::updateNotice($dataUpdateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }

    /**
     * 删除系统通知
     */
    public function deleteNotice(Request $request){
        $notice_id = $request->input('id',0);
        if ( empty($notice_id) ) return response()->error("PARAM_EMPTY");
        $dataUpdateArr = ['id'=>$notice_id,'status'=>-1];
        $result = NoticeService::updateNotice($dataUpdateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }
}